package com.xunker.appointment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.xunker.appointment.model.hospital.HospitalSet;
import com.xunker.appointment.result.Result;
import com.xunker.appointment.service.HospitalSetService;
import com.xunker.appointment.utils.MD5;
import com.xunker.appointment.vo.hospital.HospitalSetVo;
import com.xunker.appointment.vo.order.SignInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@Api(tags = "医院设置管理")
@RequestMapping("admin/hospital/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        //模拟异常
       /* try {
            int i= 10/0;
        }catch (Exception e){
            throw new ArithmeticException();
        }*/
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 根据id删除：通过浏览器访问路径无法测试
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除")
    @DeleteMapping("removeById/{id}")
    public Result removeHospitalSet(@PathVariable Long id) {
        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件分页查询
     *
     * @param current       当前页
     * @param limit         每页大小
     * @param hospitalSetVo
     * @return
     */
    @ApiOperation("分页查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospitalSet(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody(required = false) HospitalSetVo hospitalSetVo) {//hospitalSetVo可以为空
        //创建page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hospitalName = hospitalSetVo.getHospitalName();
        String hospitalCode = hospitalSetVo.getHospitalCode();
        if (!StringUtils.isNullOrEmpty(hospitalName)) {
            wrapper.like("hospital_name", hospitalName);
        }
        if (!StringUtils.isNullOrEmpty(hospitalCode)) {
            wrapper.eq("hospital_code", hospitalCode);
        }
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    /**
     * 添加医院设置
     *
     * @param hospitalSet 实体
     * @return
     */
    @ApiOperation("添加")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态1：使用
        //hospitalSet.setStatus(1);
        //签名密钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000)));
        //调用service实现类
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 根据id获取医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getById/{id}")
    public Result getHospitalSetByid(@PathVariable long id) {
        //查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation("修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b == true) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    @ApiOperation("批量删除，通过id集合")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean b = hospitalSetService.removeByIds(idList);
        if (b == true) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 医院设置的解锁与锁定
     *
     * @param id     编号
     * @param status 锁定状态 1：锁定
     * @return
     */
    @ApiOperation("医院设置的解锁与锁定")
    @PutMapping("lock/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        //修改
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b == true) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 发送签名密钥
     *
     * @param id
     * @return
     */
    @ApiOperation("发送签名密钥")
    @PutMapping("send/{id}")
    public Result sendHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hospitalCode = hospitalSet.getHospitalCode();
        //TODO 发送短信
        return Result.ok();
    }
    @GetMapping("inner/getSignInfoVo/{hospitalCode}")
    public SignInfoVo getSignInfoVo(@PathVariable("hospitalCode") String hospitalCode){
        return hospitalSetService.getSignInfoVo(hospitalCode);
    }
}
